/* This file aims to create inputs preparing for the estimation using HRRs as a market. */
********** Create "hinfo_hrr.csv"
clear all
set more off
cd "" //targed directory


forvalues i=2006/2009 {
use "./rawdata/aha_extract`i'",clear
keep id hrrcode
gen year=`i'
tempfile hrr`i'
save `hrr`i''
}


clear
use `hrr2006',clear
append using `hrr2007'
append using `hrr2008'
append using `hrr2009'
tempfile hrr
save `hrr'


clear
use "./prepare/allbasic.dta"
merge 1:1 id year using `hrr'
drop if _merge==2
drop _merge
bys id:egen hrrmode=mode(hrr)
replace hrr=hrrmode if hrr==.

outsheet ahaid year prechs adj_vnd hrr sysid using "./prepare/hinfo_hrr.csv",c replace nonames




***********************************
*********************************** Prepare inputs for IV
/* Construct IV for the reduced-form evidence */
clear
insheet using "./prepare/hinfo_hrr.csv",clear
rename (v1 v2 v3 v4 v5 v6) (ahaid year prechs adj_vnd maxhrr sysid)
keep ahaid year maxhrr
tempfile othermkt
save `othermkt'


//compute how many HSAs each hospital system involves
use "./prepare/allbasic.dta",clear
merge m:1 ahaid year using `othermkt'
keep if _merge==3
drop _merge
rename hsa hsa_old
rename maxhrr hsa


sort sysid year
egen sysyr_id=group(sysid year)
sort hsa year
egen hsayr=group(hsa year)
duplicates drop sysyr_id hsayr,force
bys sysyr_id:gen numhsa=_N
duplicates drop sysid year,force
replace numhsa=0 if sysid==0
keep sysyr_id numhsa
tempfile nhsa
save `nhsa'


//derive the markets with 1st-time-adoption stand-alone hospitals and multi-region affiliated hospitals.
//also derive the hospital systems located in the HSA above
//output the id number of the hospital systems as input to derive the internal-dominant HSA for each hospital system.
clear 
use "./prepare/allbasic.dta",clear
merge m:1 ahaid year using `othermkt'
keep if _merge==3
drop _merge
rename hsa hsa_old
rename maxhrr hsa


sort sysid year
egen sysyr_id=group(sysid year)
merge m:1 sysyr_id using `nhsa'
keep if _merge==3
drop _merge
sort hsa year
egen hsayr_id=group(hsa year)
gen sa1stadp=0
replace sa1stadp=1 if prechs==1 & adj_vnd>1 & sysid==0
bys hsayr_id:egen withsa1stadp=max(sa1stadp)
bys hsayr_id:egen withmulrsys=max(numhsa)
keep if withsa1stadp==1 & withmulrsys>1
drop if numhsa<2
duplicates drop hsayr_id sysyr_id,force
sort hsayr_id sysyr_id
outfile hsayr_id sysyr_id numhsa using "./prepare/sysyr", c replace



/* create the input of the basic information to derive the internal-dominant HSA for each hospital system. See readme in reduced_form_evidence folder for more information. */
clear
insheet using "./prepare/hinfo_hrr.csv",clear
rename (v1 v2 v3 v4 v5 v6) (ahaid year prechs adj_vnd maxhrr sysid)
keep ahaid year maxhrr
tempfile othermkt
save `othermkt'


use "./prepare/allbasic.dta",clear
merge m:1 ahaid year using `othermkt'
keep if _merge==3
drop _merge
rename hsa hsa_old
rename maxhrr hsa


sort sysid year
egen sysyr_id=group(sysid year)
sort hsa year
egen hsayr_id=group(hsa year)
sort ahaid year
egen ahayr_id=group(ahaid year)
outfile sysyr_id ahayr_id hsayr_id prechs adj_vnd bdtot sysid nprofit profit teaching ncompet ratio perc_mcr perc_mcd nhosp using "./prepare/input4createIV", c replace










